import lmd5 from 'MD5'

cc.Class({
    extends: cc.Component,

    properties: {

        faceNode:{
            default:null,
            type:cc.Node
        },

        skillNodes:{
            default:null,
            type:cc.Node
        },


        fiveNode:{
            default:null,
            type:cc.Node
        },

        modelNode:{
            default:null,
            type:cc.Node
        },

        dataLevelNode:{
            default:null,
            type:cc.Node
        },

        nameLabel:{
            default:null,
            type:cc.Label
        },
		
		jfLabel:{
            default:null,
            type:cc.Label
        },

        xwLabel:{
            default:null,
            type:cc.Label
        },

        JobLabel:{
            default:null,
            type:cc.Label
        },

        jfNode:{
            default:null,
            type:cc.Node
        },

        jobLabel:{
            default:null,
            type:cc.Label
        },

        btnStart:{
            default:null,
            type:cc.Node
        },

        btnAgain:{
            default:null,
            type:cc.Node
        },
		
		btnAgain2:{
            default:null,
            type:cc.Node
        },

        btnMain:{
            default:null,
            type:cc.Node
        },
		
		btnLand:{
            default:null,
            type:cc.Node
        },
		
		btnIntro:{
            default:null,
            type:cc.Node
        },
		
		btnAd:{
            default:null,
            type:cc.Node
        },

        btnVipAgain:{
            default:null,
            type:cc.Node
        },

        loadOldLabel:{
            default:null,
            type:cc.Node
        }
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start () {

        this.tipsmsg = '人族随机(战士、盗贼、首领)属性总和上限500\n\n妖族随机(刺客、射手、护卫)属性总和上限1000\n\n仙族随机(法师、方士、医师、魔战士)属性总和上限1500\n';

		_TIPS.setNode(this.node);
        window._RANDOM = this;
		this.ishistory = false;
		this.startPlace = null;

        this.randomOnce('ren');
        this.initButton();
		window._RANDOM.refreshJfLabel(null,null);

        
        this.refreshButtons();
        
    },

    refreshButtons:function(){

        if(!_HQM._USERNAME){
            this.btnLand.active = true;
        }else{
            this.btnLand.active = false;
        }

        /*
        if(_CHESSJFUI._ISVIP){
            this.btnAd.active = false;
            this.btnAgain2.active = true;
        }else{
            this.btnAd.active = true;
            this.btnAgain2.active = false;
        }*/

        this.refreshbtnmsg();
    },

    refreshbtnmsg:function(){

        /*
        if(!_CHESSJFUI._ISVIP){

            var superTime = _LOCALJF.getRandom();
            this.btnVipAgain.getChildByName('Background')
            .getChildByName('Label').getComponent(cc.Label)
            .string = "妖族随机("+superTime+")";

        }else{

            this.btnVipAgain.getChildByName('Background')
            .getChildByName('Label').getComponent(cc.Label)
            .string = "妖族随机";

        }*/

    },

    isGoodRole:function(){

        if(!this.randomRole){
            return false;
        }

        var goodvalue = 450;
        var npcconfig = _NPC[this.randomRole.npctype];
        if(npcconfig.race=="yao"){
            goodvalue = 900;
        }else if(npcconfig.race=="xian"){
            goodvalue = 1350;
        }

        if(this.randomRole){
            return (this.randomRole.plv+
                    this.randomRole.ilv+
                    this.randomRole.dlv+
                    this.randomRole.mlv+
                    this.randomRole.hlv)>goodvalue;
        }

        return false;

    },

    randomOnce:function(race){
        if(this.isGoodRole()){
            _CHOICEMANAGER.showChoice(this.node,'randomRole',race);
        }else{
            this.realRandom(race);
        }
    },

    realRandom:function(race){

        //console.log("是否vip:",isvip);

        this.randomRoleFunc(race);

        /*
        if(isvip){

            console.log(this.randomRole);

            this.randomRole.xwlevel={};

            this.randomRole.xw = {};

            for(var i=0;i<9;i++){

                var xwqua = _MCRD.getDropQua();

                var xw = XWH.getRandomXw();

                this.randomRole.xwlevel[xw]=xwqua;

                this.randomRole.xw[xw]=Math.floor(Math.random()*100);

            }

        }*/

        this.refreshLabel();
    },

    refreshLabel:function(){

        /*
        var aniObj = _GRM.getAttackFrame(this.randomRole.pf);
        var aniArr = [].concat(aniObj.walk).concat(aniObj.walk)
                    .concat(aniObj.attack).concat(aniObj.attack)
                    .concat(aniObj.weak).concat(aniObj.weak);
        this.modelNode.getComponent(cc.Animation).addClip(_GRM.createAniClipTime(aniArr,'walk',cc.WrapMode.Loop,8));
        this.modelNode.getComponent(cc.Animation).play('walk');
        */
       if(!_PFM.isFaceEnabel(this.randomRole.face,this.randomRole.pf)){
            this.randomRole.face = "问号";
            this.randomRole.pf = "亲卫兵";
       }

       var npcConfig = _NPC[this.randomRole.npctype];
       var rolejob = npcConfig.job;
       var rolerace = npcConfig.race;
       
        _CU.initAllAniNode(this.randomRole.pf, this.modelNode);

        this.nameLabel.string = this.randomRole.name+"\n("+this.randomRole.npctype+")";
        this.jobLabel.string = this.randomRole.job;
        this.faceNode.getComponent(cc.Sprite).spriteFrame = _GRM.getFaceFrame(this.randomRole.face);
        //this.modelNode.getComponent(cc.Sprite).spriteFrame = _GRM.getWalkFrame(this.randomRole.pf);
        //刷新职业五行种族
        this.faceNode.getChildByName('ftype').color = new cc.color(_CS.ftype[this.randomRole.ftype].color);
        this.faceNode.getChildByName('ftype').getChildByName('ftypeStr')
        .getComponent(cc.Label).string = this.randomRole.ftype;

        this.faceNode.getChildByName('job').color = new cc.color(window._JOB[rolejob].color);
        this.faceNode.getChildByName('job').getChildByName('ftypeStr')
        .getComponent(cc.Label).string = window._JOB[rolejob].sname;

        this.faceNode.getChildByName('zhong').color = new cc.color(window._RACE[rolerace].color);
        this.faceNode.getChildByName('zhong').getChildByName('ftypeStr')
        .getComponent(cc.Label).string =window._RACE[rolerace].name;

        this.refreshSkills(this.randomRole.skill);

        this.xwLabel.getComponent(cc.Label).string = XWH.getXwcz(npcConfig.npcxw,this.randomRole);

        this.JobLabel.getComponent(cc.Label).string = _JOB[npcConfig.job].desc;

        var dataArr = [
            this.randomRole.plv,
            this.randomRole.dlv,
            this.randomRole.ilv,
            this.randomRole.mlv,
            this.randomRole.hlv
        ];

        var attrNodes = this.fiveNode.getChildByName('five').getChildByName('attr');
        attrNodes.getChildByName('plv').getComponent(cc.Label).string='武:'+dataArr[0];
        attrNodes.getChildByName('dlv').getComponent(cc.Label).string='统:'+dataArr[1];
        attrNodes.getChildByName('ilv').getComponent(cc.Label).string='智:'+dataArr[2];
        attrNodes.getChildByName('mlv').getComponent(cc.Label).string='德:'+dataArr[3];
        attrNodes.getChildByName('hlv').getComponent(cc.Label).string='体:'+dataArr[4];

        var per = _MRDL.paramLength/_MRDL.maxlength;
       // _CU.drawData(graphics,dataArr,per);
        _CU.drawData(this.fiveNode
        .getChildByName('five')
        .getChildByName('data').getComponent(cc.Graphics),dataArr,per);

        this.dataLevelNode.getComponent(cc.Label).string = '总和:'+(dataArr[0]+dataArr[1]+dataArr[2]+dataArr[3]+dataArr[4]);

        this.refreshbtnmsg();
    },

    randomRoleFunc:function(race){
        this.randomRole = _CROF.getRandomMainRole(true,null,race);
    },
	

    initButton:function(){
		this.btnLand.on(cc.Node.EventType.TOUCH_START, function(){
            //_TIPS.showMessage('登陆',0);
            _QD.loginByQd('jf');	
        }, this);

        this.btnVipAgain.on(cc.Node.EventType.TOUCH_START, function(){
            
            //console.log('dddd');
            if(_CHESSJFUI._ISVIP){
                this.randomOnce('yao');
            }else{
                _TIPS.showMessage('购买vip后解锁此功能');
            }

        }, this);
		
		this.btnAd.on(cc.Node.EventType.TOUCH_START, function(){
            //_TIPS.showMessage('广告',0);
			//_QD.showLbVideo();
            _MWX.showRandomVideo();
        }, this);
		
		this.btnIntro.on(cc.Node.EventType.TOUCH_START, function(){
            _TIPS.showMessage(this.tipsmsg,0);
        }, this);
		
		
        this.btnAgain.on(cc.Node.EventType.TOUCH_START, function(){
            this.randomOnce('ren');
            window._RANDOM.refreshJfLabel(null,null);
        }, this);
		this.btnAgain2.on(cc.Node.EventType.TOUCH_START, function(){
            if(_CHESSJFUI._ISVIP){
                this.randomOnce('xian');
                window._RANDOM.refreshJfLabel(null,null);
            }else{
                _TIPS.showMessage('购买vip后解锁此功能');
            }
        }, this);
        this.btnMain.on(cc.Node.EventType.TOUCH_START, function(){
            //_TIPS.showMessage('主界面',2);
            cc.director.loadScene('index');
        }, this);
        this.btnStart.on(cc.Node.EventType.TOUCH_START, function(){
            //_TIPS.showMessage('重新开始',2);
            //this.node.getChildByName('hardSelect').active = true;
            this.gameBegin(0);
			
        }, this);

        this.node.getChildByName('hardSelect').getChildByName('mask').on(cc.Node.EventType.TOUCH_START, function(){
            //_TIPS.showMessage('重新开始',2);
            this.node.getChildByName('hardSelect').active = false;
			
        }, this);

        this.node.getChildByName('hardSelect').getChildByName('ui')
        .getChildByName('btnEasy')
        .on(cc.Node.EventType.TOUCH_START, function(){
            this.gameBegin(0);
			
        }, this);

        this.node.getChildByName('hardSelect').getChildByName('ui')
        .getChildByName('btnHard')
        .on(cc.Node.EventType.TOUCH_START, function(){
            this.gameBegin(1);
        }, this);

        this.node.getChildByName('hardSelect').getChildByName('ui')
        .getChildByName('btnCrasy')
        .on(cc.Node.EventType.TOUCH_START, function(){
            this.gameBegin(2);
        }, this);

        var labels = this.loadOldLabel.getChildByName('label').getChildren();

        this.loadOldLabel.getChildByName('btnBegin').on(cc.Node.EventType.TOUCH_START, function(){
            window._JUMPSENCE = "map";
            cc.director.loadScene('jump');

        }, this);

        this.loadOldLabel.getChildByName('mask').on(cc.Node.EventType.TOUCH_START, function(){
            this.loadOldLabel.active = false;
        }, this);

        var skillNodeArr = this.skillNodes.getChildren();
        for(var i=0;i<skillNodeArr.length;i++){
            var skillNode = skillNodeArr[i];
            skillNode.on(cc.Node.EventType.TOUCH_START, function(){
                if(this.skillname&&_SKILL[this.skillname]){
                    _TIPS.showMessage(_SKILL[this.skillname].des);
                }else if(this.skillname&&_SKILLBD[this.skillname]){
                    _TIPS.showMessage(_IOF.getAttrString(_SKILLBD[this.skillname].attr));
                }
            }, skillNode);
        }

        for(var i=0;i<labels.length;i++){

            var loaddatabtn = labels[i].getChildByName('btns').getChildByName('btnRead');
            loaddatabtn.olddataindex = i;
            loaddatabtn.randomsence = this;
            loaddatabtn.on(cc.Node.EventType.TOUCH_START, function(){
                this.randomsence.loadOldAndBegin(this.olddataindex);
            }, loaddatabtn);

        }
    },


    gameBegin:function(hdlv){
        
        this.randomRole.exp = _EXPMANAGER.getExpByLevel(5);
        var npcConfig = _NPC[this.randomRole.npctype];
        var rolejob = npcConfig.job;
        var equipArr = _MCFC.getEquipByJobAndRole(rolejob,5);
        if(equipArr&&equipArr.length>0){
            for(var i=0;i<equipArr.length;i++){
                this.randomRole.equip.push(_MCFC.createEquipByTypeAndDz(equipArr[i],0,'A'));
            }
        }


        var roleData = _ROLEDATA.getRoleDataOfPlayer(this.randomRole);
        this.randomRole.isMainRole = 1;
        //this.randomRole.skill.push('主角光环');
        //this.randomRole.skill.push('晓之以理');
        //this.randomRole.skill.push('铁拳');
        //this.randomRole.skill.push('ATM之怒');
        //this.randomRole.skill.push('迫击炮');
        this.randomRole.exp = 400;
        this.randomRole.hp = roleData.maxHp;
        this.randomRole.mg = roleData.maxMagic;
        window.gameData = _GAMEDATAHELPER.getInitData(this.randomRole);
        window.gameData.hdlv = hdlv;

        //点亮修为图鉴
        _GAMEDATAHELPER.signXwTjByRole(window.gameData.playerTeam[0]);
        //点亮图鉴
        _GAMEDATAHELPER.signNpcTj(window.gameData.playerTeam[0].npctype);
        //window.gameData.workList.push(_MXYT.getMainTaskByName('我是谁，我在哪？'));
        _MXYT.pushTask('五台山之战');

        //_TM.addAll();//仓库放入所有资源 测试用
        if(!this.isLoadOldData()){

            if(_QD._qdcode==4){
                this.randomRole.roleid = new Date().getTime();
                _HQM.uploadRole(
                    this.randomRole,
                    lmd5.hex_md5(_HQM._ACT+"_"
                                +_HQM._USERNAME+"_"
                                +_HQM._SERVERID+"_"
                                +this.randomRole.roleid+"_"
                                +"0_"
                                +this.randomRole.plv+"_"
                                +_HQM._VIP+"_"
                                +_HQM._KEY)
                    );
            }
            window._JUMPSENCE = "map";
            cc.director.loadScene('jump');
        }else{
            this.showLoadOldData();
        }
    },


    isLoadOldData:function(){

        this.oldData = _MSD.getPassSaveData();

        if(this.oldData&&this.oldData.length>0){

            this.showLoadOldData();

            return true;
        }

        return false;

    },

    showLoadOldData:function(){

        this.loadOldLabel.active = true;

        if(_QD.isloadAllSaveData()){
            this.loadOldLabel.getChildByName('tips').active = false;
        }

        var labels = this.loadOldLabel.getChildByName('label').getChildren();

        for(var i=0;i<labels.length;i++){

            if(this.oldData&&this.oldData[i]){
                labels[i].active = true;
                labels[i].getChildByName('title').getComponent(cc.Label).string = _MSD.getDataTitLeByData(this.oldData[i],(i+1));
            }else{
                labels[i].active = false;
            }

        }

    },

    loadOldAndBegin:function(olddataindex){

        var olddata = this.oldData[olddataindex];

        if(!olddata.fsbhdlv||olddata.fsbhdlv==0){
            window.gameData.fsbhdlv=1;
        }else{
            window.gameData.fsbhdlv=olddata.fsbhdlv+1;
            if(window.gameData.fsbhdlv>=9){
                window.gameData.fsbhdlv=9;
            }
        }

        //继承队伍
        for(var i=0;i<olddata.playerTeam.length;i++){
            window.gameData.playerTeamBack.push(olddata.playerTeam[i]);
        }

        window.gameData.gold = olddata.gold;
        window.gameData.npctj = olddata.npctj;
        if(_CHESSJFUI._ISVIP){
            window.gameData.fightItem = olddata.fightItem;
            window.gameData.equipItem = olddata.equipItem;
            window.gameData.specilItem = olddata.specilItem;
            window.gameData.skillItem = olddata.skillItem;
            window.gameData.materialItem = olddata.materialItem;
        }
        window._JUMPSENCE = "map";
        cc.director.loadScene('jump');

    },

	
	refreshJfLabel:function(isvip,score){
		var localjf = _LOCALJF.getJIFEN();
		var localrandom = _LOCALJF.getRandom();
		var result = "是否VIP:"+(_MANAGERJF._ISVIP?'是':'否');
		result+="\n本地积分:"+(localjf?localjf:'0');
		result+="\n随机次数:"+(localrandom?localrandom:0);
		this.jfLabel.string = result;
	},

    refreshSkills:function(skillArr){
        var skillNodeArr = this.skillNodes.getChildren();
        for(var i=0;i<skillNodeArr.length;i++){
            var skillNode = skillNodeArr[i];
            if(skillArr[i]){
                skillNode.skillname = skillArr[i];
                _MRDL.refreshSkill(skillNode,skillArr[i]);
            }else{
                skillNode.active = false;
            }
        }

    }

    // update (dt) {},
});
